package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.time.LocalDate;

/**
 * @ClassName DatePickerDemo
 * @Description
 * @Author wangss
 * @date 2020.05.26 09:42
 * @Version 1.0
 */
public class DatePickerDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        DatePicker dp = new DatePicker(LocalDate.now());
        dp.setEditable(false);

        anchorPane.getChildren().add(dp);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        dp.valueProperty().addListener(new ChangeListener<LocalDate>() {
            @Override
            public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue) {
                int year = newValue.getYear();
                int month = newValue.getMonthValue();
                int day = newValue.getDayOfMonth();
                int date = newValue.getDayOfYear();
                int week = newValue.getDayOfWeek().getValue();

                System.out.println("year = " + year);
                System.out.println("month = " + month);
                System.out.println("day = " + day);
                System.out.println("date = " + date);
                System.out.println("week = " + week);
            }
        });

        // 自定义日历样式，平常没用
        /*dp.setDayCellFactory(new Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(DatePicker param) {
                return new DateCell() {
                    @Override
                    public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);
                        this.setGraphic(new Label(LocalDate.now().toString()));
                    }
                };
            }
        });*/
    }
}
